<template>
  <div>
    <!-- 导航栏部分 -->
    <van-nav-bar
      title="就诊人管理"
    
      left-arrow
      border
      @click-left="back"
      fixed
    />
    <van-form style="margin-top: 46px">
      <van-field
        v-model="name"
        name="用户名"
        label="姓名"
        placeholder="请输入姓名"
        @blur="checkname"
      />
      <van-field
        v-model="patient_number"
        type="text"
        name="就诊卡号"
        label="就诊卡号"
        placeholder="请输入就诊卡号"
        @blur="checkpat_num"
      />
      <van-field
        v-model="id_number"
        type="text"
        name="身份证号"
        label="身份证号"
        placeholder="请输入身份证号"
        @blur="checkid_num"
      />
      <van-field
        v-model="phone"
        type="text"
        name="手机号"
        label="手机号"
        placeholder="请输入手机号"
        @blur="checkphone_num"
      />
      <van-field
        v-model="relation"
        type="text"
        name="与用户关系"
        label="与用户关系"
        placeholder="请输入关系"
        
      />
      <van-field
        v-model="address"
        type="text"
        name="地址"
        label="地址"
        placeholder="请输入地址"
      />
      <div style="margin: 16px">
        <van-button round block type="info" @click="give" color="#1074ff">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      name: "",
      patient_number: "",
      address: "",
      phone: "",
      id_number: "",
      relation: ''
    };
  },
  methods: {
    checkphone_num() {
      // 判断是否为手机号
      let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
      if (!reg.test(this.phone)) {
        this.$toast.fail("手机号号格式错误");
        return false;
      } else {
        return true;
      }
    },
    checkpat_num() {
      // 判断是否为16位纯数字
      let reg = /^[0-9]{16}$/;
      if (!reg.test(this.patient_number)) {
        this.$toast.fail("就诊卡号格式错误");
        return false;
      } else {
        return true;
      }
    },
    checkid_num() {
      // 判断是否为18位全数字
      let reg = /^[0-9]{18}$/;
      if (!reg.test(this.id_number)) {
        this.$toast.fail("身份证格式错误");
        return false;
      } else {
        return true;
      }
    },
    checkname() {
      // 正则判断是否全为中文,如果是中文返回true
      let reg = /^[\u4E00-\u9FA5]+$/;
      if (!reg.test(this.name)) {
        this.$toast.fail("姓名格式错误");
        return false;
      } else {
        return true;
      }
    },
    give() {
      if (
        this.checkname() &&
        this.checkid_num() &&
        this.checkpat_num() &&
        this.checkphone_num()
      ) {
        let url = `${this.$base}patient/add_patient`;
        let id = sessionStorage.getItem('uid')
        let params = `name=${this.name}&&id_number=${this.id_number}&&relation=${this.relation}&&address=${this.address}&&phone=${this.phone}&&patient_number=${this.patient_number}&&uid=${id}`;
        this.axios.post(url, params).then((res) => {
          // console.log(res);
          if (res.data.code == 200) {
            this.$toast.success("注册成功");
            this.$router.push('/patientmanagement')
          }
        });
      } else {
        this.$toast.fail("请检查信息");
      }
    },
    back() {
      this.$router.go("-1");
    },
  },
};
</script>

<style lang="less" scoped>
.van-nav-bar {
  background-color: #1074ff;

  /deep/ .van-nav-bar__title {
    color: #fff;
  }
  /deep/ .van-icon-arrow-left {
    color: #fff;
  }
}
button {
  background-color: #01d99d;
  border: none;
}
</style>